package stardust.product.platform.persistence;


import java.sql.SQLException;

import stardust.product.platform.persistence.transaction.TransactionManager;
import stardust.product.platform.persistence.transaction.TransactionObject;


public class DataAccess
{
	
	private DataAccessFactory dataAccessFactory;
	
	protected DataAccess(DataAccessFactory dataAccessFactory)
	{
		this.dataAccessFactory = dataAccessFactory;
	}
	
	public void commit() throws PersistenceException, SQLException
	{
		this.getTransactionManager().commit();
	}
	
	public void rollback() throws PersistenceException, SQLException
	{
		this.getTransactionManager().rollback();
	}
	
	public void close() throws PersistenceException, SQLException
	{
		this.getTransactionManager().rollback();
	}

	public JdbcHelper createJdbcHelper()
	{
		TransactionObject transactionObject;
		
		if ((transactionObject = this.getTransactionManager().getTransaction()) == null)
		{
			try
			{
				transactionObject = this.getTransactionManager().beginTransaction();
			}
			catch (PersistenceException e)
			{
				throw new PersistenceException("无法开启事务。", e);
			}
			catch (SQLException e)
			{
				throw new PersistenceException("无法开启事务。", e);
			}
		}
		
		return new JdbcHelper(transactionObject.getConnection());
	}
	
	public TransactionManager getTransactionManager()
	{
		return this.dataAccessFactory.getTransactionManager();
	}
	
}
